package com.lvmama.pangolin.lmm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.lvmama.pangolin.common.RowkeyGenerator;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.ProductConditionVO;
import com.lvmama.pangolin.entity.lmm.LMMTicketCommodity;
import com.lvmama.pangolin.entity.lmm.LMMTicketProduct;
import com.lvmama.pangolin.lmm.service.LMMTicketProductService;
import com.lvmama.pangolin.mybatis.mapper.lmm.LMMTicketCommodityMapper;
import com.lvmama.pangolin.mybatis.mapper.lmm.LMMTicketProductMapper;

@Transactional
@Service
public  class LMMTicketProductServiceImpl implements LMMTicketProductService
{
	@Autowired
	private LMMTicketProductMapper ticketProductMapper;
	@Autowired
	private LMMTicketCommodityMapper ticketCommodityMapper;
//	@Autowired
//	private LMMTicketCommodityPriceMapper ticketCommodityPriceMapper;
	@Override
	public int deleteByPrimaryKey(String id)
	{
		return ticketProductMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteByByProdId(String product_id)
	{
		return ticketProductMapper.deleteByByProdId(product_id);
	}

	@Override
	public int insert(LMMTicketProduct record)
	{
		return ticketProductMapper.insert(record);
	}

	@Override
	public int insertSelective(LMMTicketProduct record)
	{
		return ticketProductMapper.insertSelective(record);
	}

	@Override
	public LMMTicketProduct selectByPrimaryKey(String id)
	{
		return ticketProductMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<LMMTicketProduct> selectByProdId(String product_id)
	{
		return ticketProductMapper.selectByProdId(product_id);
	}
	@Override
	public LMMTicketProduct selectByProductId(String product_id)
	{
		return ticketProductMapper.selectByProductId(product_id);
	}

	@Override
	public int updateByPrimaryKeySelective(LMMTicketProduct record)
	{
		return ticketProductMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByProdIdSelective(LMMTicketProduct record)
	{
		return ticketProductMapper.updateByProdIdSelective(record);
	}

	@Override
	public int updateByPrimaryKey(LMMTicketProduct record)
	{
		return ticketProductMapper.updateByPrimaryKey(record);
	}

	/**
	 * 根据产品id 产品名称  城市 和业务编码进行分页查询
	 * @param pc
	 * @return
	 */
	@Override
	public List<LMMTicketProduct> getAllList(ProductConditionVO pc) {
		PageHelper.startPage(pc.getPage(), pc.getPageSize());
		List<LMMTicketProduct> list = new ArrayList<>();
		list = ticketProductMapper.getAllList(pc);
        //PageInfo<LMMTicketProduct> page = new PageInfo<LMMTicketProduct>(list);
		return list;
	}

	@Override
	public LMMTicketProduct selectByCompany(String company) {
		
		return ticketProductMapper.selectByCompany(company);
	}

	@Override
	public List<LMMTicketProduct> getAllList() {
		return ticketProductMapper.getAllList();
	}

	/**
	 * 保存商品和产品信息
	 */
	@Override
	public void saveProductInfo(LMMTicketProduct prod,List<LMMTicketCommodity> commodityList, Date date)
	{
		int ret = ticketProductMapper.updateByProdIdSelective(prod);
		String prodId = prod.getProductId();
		date = date == null ? new Date():date;
		if(ret > 0)
		{
			List<LMMTicketProduct> prodList  = ticketProductMapper.selectByProdId(prod.getProductId());
			
			if(prodList != null && !prodList.isEmpty())
			{
				String  id = prodList.get(0).getId();
				if(commodityList != null && !commodityList.isEmpty())
				{
//					List<LMMTicketCommodityPrice> priceList  = null;
					LMMTicketCommodity  commodityt = null;
					for (LMMTicketCommodity commodity : commodityList)
					{
						commodityt = ticketCommodityMapper.selectByProdCommId(prodId, commodity.getCommodityId());
						if(commodityt != null )
						{
							if(commodityt.getCommPriceChangeNum() == null)
							{
								commodityt.setCommPriceChangeNum(0);
							}
							if(StringTools.isValid(commodityt.getSalePrice()))
							{
								if(!commodityt.getSalePrice().equals(commodity.getSalePrice()))
								{
									commodity.setCommPriceChangeNum(commodityt.getCommPriceChangeNum() + 1);
								}
							}
							else if(StringTools.isValid(commodity.getSalePrice()))
							{
								commodity.setCommPriceChangeNum(commodityt.getCommPriceChangeNum() + 1);
							}
							
						}
						
						commodity.setId(id);
						commodity.setCreateTime(null);
						int count = ticketCommodityMapper.updateByPrimaryKeySelective(commodity);
						String priceId = null;
						if(count > 0)
						{
//							LMMTicketCommodity qc = ticketCommodityMapper.selectByPrimaryKey(id, commodity.getCommodityId());
//							priceId = qc.getPriceListId();
						}
						else
						{
							
							priceId = RowkeyGenerator.generateRowkey(
									commodity.getCommodityId(), date, prodId);
							//生成商品时间价格表id
							commodity.setId(id);
							commodity.setPriceListId(priceId);
							commodity.setCreateTime(new Date());
							ticketCommodityMapper.insert(commodity);
							
						}
						/**
						 * 商品时间价格表不入mysql,入hbase,代码注释
						priceList = commodity.getPriceList();
						if (priceList != null && !priceList.isEmpty())
						{
							ticketCommodityPriceMapper.deleteByIdAndTask(priceId,taskId);
							for (LMMTicketCommodityPrice commodityPrice : priceList)
							{
								commodityPrice.setId(priceId);
								ticketCommodityPriceMapper.insert(commodityPrice);
							}
						}
						**/
						
						
					}
					
				}
			}
			
		}
		else
		{
			// 生成唯一id
			String id = RowkeyGenerator.generateRowkey(prodId, date, prod.getTaskId());
			prod.setId(id);
			ticketProductMapper.insert(prod);
			if (commodityList != null && !commodityList.isEmpty())
			{
				// 默认删除该产品的商品所有数据，有无都做一次删除
//				ticketCommodityPriceMapper.deleteByProdId(prodId);
				ticketCommodityMapper.deleteByProdId(prodId);
//				List<LMMTicketCommodityPrice> priceList = null;
				for (LMMTicketCommodity commodity : commodityList)
				{
					String priceId = null;
					
					priceId = RowkeyGenerator.generateCommodityRowkey(
							commodity.getCommodityId(), date, prod.getProductId());
					//生成商品时间价格表id
					
					commodity.setId(id);
					commodity.setPriceListId(priceId);
					commodity.setCreateTime(new Date());
					ticketCommodityMapper.insert(commodity);
					/**
					 * 商品时间价格表不入mysql,入hbase,代码注释
					priceList = commodity.getPriceList();
					if (priceList != null && !priceList.isEmpty())
					{
						
						for (LMMTicketCommodityPrice commodityPrice : priceList)
						{
							commodityPrice.setId(priceId);
							ticketCommodityPriceMapper.insert(commodityPrice);
						}
					}
					**/
				}
			}			
			
			
		}
		
	}
	
}
